
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guidatatag;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

public class FetchTextFromWebPage 
{
	Reader r;
	String path;
	File HTMLFile;
	HtmlTextParser htmltextparser;
	
	public FetchTextFromWebPage(String path) 
	{
		HTMLEditorKit.Parser parser;					
        HTMLFile = new File (path);          
        byte[] html = new byte[new Long(HTMLFile.length()).intValue()];
        
        try
        {
            FileInputStream fis = new  FileInputStream(HTMLFile);           
            try 
            {
				fis.read(html);
			} 
            catch (IOException e) 
            {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            ByteArrayInputStream ba_read = new ByteArrayInputStream(html);
            r = new InputStreamReader(ba_read);
        }
        catch(FileNotFoundException e)
        {
        	URL url = null;
			try 
			{
				url = new URL(path);				
				URLConnection connection = null;
				
				try 
				{
					connection = url.openConnection();					
					connection.setDoInput(true);
					InputStream inStream = null;
					
					try 
					{
						inStream = connection.getInputStream();						
						BufferedReader input = new BufferedReader(new InputStreamReader(inStream));
						StringBuffer sbr = new StringBuffer();
						String line = null;
						
						try 
						{
							while ((line=input.readLine()) != null)
							{
								System.out.println(line);
								sbr.append(line+"\n");
							}
						} 
						catch (IOException e1) 
						{
								// TODO Auto-generated catch block
								e1.printStackTrace();
						}
						
						File temp = new File("//home//wiki14//temp.txt");
						if(!temp.exists())
						{
							if(!temp.createNewFile())
								System.out.println("File caanot be created");
						}
						else
						{
							temp.delete();
							if(!temp.createNewFile())
								System.out.println("File caanot be created");
						}
						
						java.io.FileWriter fw = new java.io.FileWriter(temp);
						fw.write(sbr.toString());
						fw.close();
						
						//byte[] temparr = new byte[new Long(temp.length()).intValue()];
						byte [] newbyte = new byte[new Long(sbr.length()).intValue()];
						ByteArrayInputStream ba_read = new ByteArrayInputStream(newbyte);
			            r = new InputStreamReader(ba_read);	        
					}
					catch (IOException e1)
					{
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}					
				}
				catch (IOException e1) 
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}	        	 
			} 
			catch (MalformedURLException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}        	 
        }
        
        parser = new ParserDelegator();        
        htmltextparser = new HtmlTextParser();    
        htmltextparser.initData();
        
        try 
        {
        	//parse method Parses The HTML with overideen Mehods of class HTMLEditorKit.ParserCallback
			parser.parse(r,htmltextparser , true);
		}
        catch (IOException e) 
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}                   
        
        try 
        {
			r.close();
		} 
        catch (IOException e)
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 	
	}
	
	public String getData()
	{
		return htmltextparser.getData();        
	}
	
	public static void main(String[] args) 
	{
		FetchTextFromWebPage fetch = new FetchTextFromWebPage("//home//wiki14//temp.txt");
		//FetchTextFromWebPage fetch = new FetchTextFromWebPage("http://en.wikipedia.org/wiki/Technology");
				
		try
		{
			File temp = new File("//home//wiki14//webtext.txt");
			if(!temp.exists())
			{
				if(!temp.createNewFile())
					System.out.println("File caanot be created");
			}
			else
			{
				temp.delete();
				if(!temp.createNewFile())
					System.out.println("File caanot be created");
			}
			
			java.io.FileWriter fw = new java.io.FileWriter(temp);
			fw.write(fetch.getData());
			fw.close();
		}
		catch (Exception e) 
		{
			// TODO: handle exception
		}
	}
}
